<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>新增模块</title> {{>/WEB-INF/pages/common/meta_base.part.html}}
{{>/WEB-INF/pages/common/refs_base.part.html}}
{{>/WEB-INF/pages/common/refs_plupload.part.html}}
</head>
<body>
	<!-- <div class="container"> -->
	{{>/WEB-INF/pages/common/layout_header.part.html}}

	<!--Con//Start-->
	<div class="container">
		<ul class="breadcrumb">
			<li><a href="{{ctx}}">首页</a></li>
			<li><a href="{{ctx}}/mam/widget/list-view">模块管理</a></li>
			<li class="active">新增模块</li>
		</ul>
		<hr class="hr1" />
		<div class="hr-10"></div>
		<div class="container bor-tab bg-st-white">
			<div class="mar-st-a30">
				<div class="bor-bot pad-st-b2">
					<div class="row">
						<div class="col-6 text-left tit4-st">模块基本信息</div>
						<div class="col-6 text-right"></div>
					</div>
					<div class="hr"></div>
				</div>
				<form id="dataForm" name="dataForm" method="post"
					action="{{ctx}}/mam/widget/add" enctype="multipart/form-data">
					<div>
						<input name="wVersion[0].dependsOn" class="input-common"
							type="hidden" id="dependsOn" value="" /> <input
							name="wVersion[0].size" class="input-common" type="hidden"
							id="size" value="" /> <input name="wVersion[0].release_not"
							class="input-common" type="hidden" id="release_not" value="" />
						<input name="checkIdentify" class="input-common" type="hidden"
							id="showResult" value="" /> <input class="input-common"
							type="hidden" id="name_showResult" value="" /> <input
							class="input-common" type="hidden" id="autoShowResult" value="" />
					</div>
					<div class="mar-st-a30">
						<table width="90%" border="0" align="center" cellpadding="0"
							cellspacing="0" class="tab-td-pa">
							<tr>
								<td width="30%" align="right" for="focusedInput">客户端内置模块：</td>
								<td class="text-left"><select name="localSelect"
									onchange="localSelectCh(this.value)"
									class="form-control  wid50" id="localSelect">
										<option value="0">否</option>
										<option value="1">是</option>
								</select></td>
							</tr>
							<tr>
								<td width="30%" align="right" for="focusedInput">应用权限：</td>
								<td class="text-left"><select name="appAuth"
									class="form-control wid50" id="appAuth">
										<option value="false">否</option>
										<option value="true">是</option>
								</select></td>
							</tr>
							<tr>
								<td align="right" class="bd_txt">模块类别：</td>
								<td class="text-left"><select name="category_id"
									btvd-type="required" id="category_id"
									class="form-control  wid50"> {{#list}}
										<option value="{{id}}">{{name}}</option> {{/list}}
								</select></td>
							</tr>
							<tr id="packUploadContainertr" style="display:">
								<td width="30%" align="right" for="focusedInput">模块包：</td>
								<td class="text-left">
									<div id="packUploadContainer"></div>
								</td>
							</tr>
							<tbody id="toboyId" style="display: none">
								<tr>
									<td width="30%" align="right" for="focusedInput">模块名称：</td>
									<td class="text-left"><input class="form-control wid50"
										onchange="checkName()" name="name" type="text" value=""
										id="name" btvd-type="required" /></td>
								</tr>
								<tr>
									<td width="30%" align="right" for="focusedInput">模块标识：</td>
									<td class="text-left"><input class="form-control wid50"
										type="text" name="identify" value="" id="identifier"
										readonly="readonly" onblur="checkedIdentifyer()"
										btvd-type="required" /></td>
								</tr>
								<tr id="localtr" style="display: none">
									<td width="30%" align="right" for="focusedInput">local标识：</td>
									<td class="text-left"><input class="form-control wid50"
										type="text" name="local" value="" id="local" /></td>
								</tr>
								<tr>
									<td width="30%" align="right" for="focusedInput">模块版本：</td>
									<td class="text-left"><input class="form-control wid50"
										readonly="readonly" name="wVersion[0].version" value=""
										id="version" btvd-type="required" /></td>
								</tr>
								<tr>
									<td width="30%" align="right" for="focusedInput">构建号：</td>
									<td class="text-left"><input class="form-control wid50"
										name="wVersion[0].build" value="" readonly="readonly"
										id="build" btvd-type="required" /></td>
								</tr>
								<tr id="iconUploadContainertr" style="display: none">
									<td width="30%" align="right" for="focusedInput">模块图标：</td>
									<td class="text-left"><div id="iconUploadContainer"></div></td>
								</tr>
								<tr>
									<td width="30%" align="right" for="focusedInput">是否隐藏：</td>
									<td class="text-left"><input type="radio"
										class="radio-inline" onclick="isShowCheck()" name="hidden"
										id="hiddenYES" value="true" />隐藏 <input type="radio"
										class="radio-inline" name="hidden" id="hiddenNO" value="false"
										checked="checked" onclick="isShowCheck()" />显示</td>
								</tr>
								<tr id="autoDownloadtr" style="display:">
									<td width="30%" align="right" for="focusedInput">是否自动下载：</td>
									<td class="text-left"><input type="radio"
										class="radio-inline" name="autoDownload" id="autoDownloadNO"
										value="false" checked="checked" />否 <input type="radio"
										class="radio-inline" name="autoDownload" id="autoDownloadYES"
										value="true" />是</td>
								</tr>
								<tr id="isAutoShowChecktr" style="display:">
									<td width="30%" align="right" for="focusedInput">是否自动打开：</td>
									<td class="text-left"><input type="radio"
										class="radio-inline" name="autoShow" id="isAutoShowNO"
										value="false" checked="checked" onclick="isAutoShowCheck()" />否
										<input type="radio" class="radio-inline" name="autoShow"
										id="isAutoShowYes" value="true" onclick="isAutoShowCheck()" />是</td>
								</tr>
								<tr id="timeUnit_isAutoShowYes" style="display: none">
									<td width="30%" align="right" for="focusedInput">单位时间：</td>
									<td class="text-left"><select name="timeUnit"
										id="timeUnit" class="form-control  wid50">
											<option value="H">小时</option>
											<option value="M">分钟</option>
											<option value="S">秒</option>
									</select></td>
								</tr>
								<tr id="showIntervalTime_isAutoShowYes" style="display: none">
									<td width="30%" align="right" for="focusedInput">时间间隔：</td>
									<td class="text-left"><input class="form-control wid50"
										name="showIntervalTime" value="0" id="showIntervalTime"
										btvd-type="number" /></td>
								</tr>
								<tr>
									<td width="30%" align="right" for="focusedInput">权重值：</td>
									<td class="text-left"><input class="form-control wid50"
										name="sortingWeight" value="1" id="sortingWeight"
										btvd-type="number" /></td>
								</tr>
								<tr>
									<td align="right" class="control-group">模块简介：</td>
									<td colspan="3" class="text-left"><textarea
											id="discription" class="form-control wid50"
											name="discription" cols="" rows=""></textarea></td>
								</tr>
							</tbody>
						</table>
					</div>

					<div class="hr-10"></div>
					<div class="text-center">
						<button type="button" class="btn btn-primary btn-small tls"
							id="submitBut">保存</button>
						<button type="button" class="btn btn-common btn-small tls"
							onclick="javascript:history.back();">返回</button>
					</div>
				</form>
			</div>
			<div class="hr-20"></div>
		</div>
	</div>
	{{>/WEB-INF/pages/common/layout_footer.part.html}}
</body>
</html>
<script type="text/javascript">
	$('#dataForm').validation();
	$("#localSelect option[value='0']").attr("selected", true);
	localSelectCh("0");

	var popover = function(inputId, content) {
		$('#' + inputId).attr("data-placement", "bottom");
		$('#' + inputId).attr("data-content", content);
		$('#' + inputId).attr("data-toggle", "popover");
		$('#' + inputId).popover('show');
	}
	$(function() {
		var uploader = buildPlupload({
			id : "packUploadContainer",
			elmName : "wVersion[0].boundle",
            filters : [{title : "模块包", extensions : "zip"}],
            tips:"请上传zip包",
			targetUrl : "{{ctx}}/mam/attachment/upload",
			defaultVal : "",
			btnName : "上传模块包"
		});

		uploader.bind('FileUploaded', function(up, files, info) {
			var obj = eval("(" + info.response + ")");
			var id = obj.id;
			$('#packUploadContainerInput').val(id);
			var size = $('#packUploadContainerSize').val();
			$('#size').val(size);
			readFileAction(id);

		});

		$("#submitBut").click(function() {
			if($('#packUploadContainerInput').val()==""){
				popover("packUploadContainerBtn", "请上传zip包！");
				return false;
			}
			if($('#autoShowResult').val()!=""){
				updateAutoShow($('#autoShowResult').val());
				return false;
			}
			
			$("#dataForm").submit();
		})

	});

	//更新已经设置自动打开模块的状态，设置为不自动打开
	function updateAutoShow(widgetId) {
		var data={};
		data.id=widgetId;
		$.ajax({
			type : "POST",
			dataType : "json",
			url : "{{ctx}}/mam/widget/autoShowSatus",
			data:data,
			success : function(result, textStatus) {
				if (result.result == "success") {
					$("#dataForm").submit();
				} else {
					bootbox.alert("保存失败，请稍后再试！");
				}
			},
			error : function(data, status, e) {
				bootbox.alert("系统繁忙，请稍后再试！");
			}
		});
				
	}
	
	//检测模块标识是否被占用
	function checkedIdentifyer() {
		var identify = jQuery("#identifier").val();
		$.ajax({
			async : true,
			type : "GET",
			url : "{{ctx}}/mam/widget/checkIdentify?identify=" + identify,
			dataType : 'json',
			success : function(data, textStatus) {
				if (data.result == "1") {
					$("#identifier").attr("btvd-err","该模块标识已经被占用，请换别的试试！");
				} else {
					$("#identifier").removeAttr("btvd-err");
				}
			}
		});
	}

	//读取模块配置文件
	function readFileAction(id) {
		$.ajax({
			async : true,
			type : "GET",
			url : "{{ctx}}/mam/attachment/readfile/" + id,
			dataType : 'json',
			success : function(data, textStatus) {
				if(data.result=="success"){
					jQuery("#identifier").val(data.identifier);
					jQuery("#name").val(data.name);
					jQuery("#version").val(data.version);
					if(data.build==null||data.build==""){
						jQuery("#build").val("1");
					}else{
						jQuery("#build").val(data.build);
					}
					jQuery("#discription").val(data.releaseNote);
					jQuery("#iconUploadContainerInput").val(data.icon_id);
					jQuery("#dependsOn").val(data.dependsOn);
					jQuery("#release_not").val(data.releaseNote);
					if (data.hidden == "true") {
						jQuery("#hiddenYES").attr("checked", "true");
					} else {
						jQuery("#hiddenNO").attr("checked", "true");
					}
					$('#toboyId').show();
					checkedIdentifyer();
					checkName();
					$('#iconUploadContainer').html("");
					var icon = data.icon_id;
					if (icon == null) {
						icon = "";
					}
					var uploader1 = buildPlupload({
						id : "iconUploadContainer",
						elmName : "icon_id",
						targetUrl : "{{ctx}}/mam/attachment/icon",
						defaultVal : icon,//设置图标
						btnName : "上传图标"
					});
				}else {
					popover("packUploadContainerBtn", "模块包上传有误，请修改！");
				}
			}
		});
	}

	//名称不能包含特殊字符验证
	function checkName() {
		var name = $("#name").val();
		
		if(name==""){
			 $("#name").attr("btvd-type","required");
			 $("#name").removeAttr("btvd-err");
				return;
		}
		var tel =/^[^`~!@#$%^&*()+=|\\\][\]\{\}:;'\,.<>/?]{1}[^`~!@$%^&()+=|\\\][\]\{\}:;'\,.<>?]{0,19}$/; 
		var flag = tel.test(name);
		if (!flag) {
			$("#name").attr("btvd-type","noSpecialChar");
			return ;
		}
		$.ajax({
			type : "POST",
			dataType : "json",
			url : "{{ctx}}/mam/widget/checkName",
			data : "checkName=" + name,
			success : function(data, textStatus) {
				if (data.result == "true") {
					$("#name").attr("btvd-err","该名称已经被占用，请换别的试试！");
				} else {
					$("#name").removeAttr("btvd-err");
				}

			}
		});

	}
	
	//判断是否隐藏
	function isShowCheck(){
		var autoShow = $('#hiddenYES');
		if(autoShow.is(':checked')){
			$('#isAutoShowChecktr').hide();
			$("input[name='autoShow']").get(0).checked=true; 
			$('#timeUnit_isAutoShowYes').hide();
			$('#showIntervalTime_isAutoShowYes').hide();
		}else {
			$('#isAutoShowChecktr').show();
		}
		if($('#localSelect').val()==="1"){//是本地模块
			$('#isAutoShowChecktr').hide();
		}
	}
	
	//判断是否选择自动打开选项
	function isAutoShowCheck(){
		var autoShow = $('#isAutoShowYes');
		if(autoShow.is(':checked')){
			$.ajax({
				type : "GET",
				dataType : "json",
				url : "{{ctx}}/mam/widget/autoShow",
				success : function(data, textStatus) {
					if (data.result == "true") {
						bootbox.confirm(
								"已经存在自动打开模块，您确定要将此模块设置为自动打开吗?",
								function(result) {
										if (result) {
											$('#autoShowResult').val(data.id);
										}else{
											$("input[name='autoShow']").get(0).checked = true;
										}
									});
						}			
					} 
			});
			$('#timeUnit_isAutoShowYes').show();
			$('#showIntervalTime_isAutoShowYes').show();
		}else {
			$('#timeUnit_isAutoShowYes').hide();
			$('#showIntervalTime_isAutoShowYes').hide();
			$('#autoShowResult').val("");
		}
	}
	
	
	//本地模块过滤
	function localSelectCh(value){
		if(value=="1"){
			$("#packUploadContainerInput").val("1");
			$('#identifier').removeAttr("readonly");
			$("#version").val("1.0.0.0");
			$("#build").val("0");
			$('#size').val("0");
			$('#local').attr("btvd-type","required");
			$('#toboyId').show();
			$('#localtr').show();
			$('#autoDownloadtr').hide();
			$('#isAutoShowChecktr').hide();
			$('#packUploadContainertr').hide();
			$('#iconUploadContainertr').hide();
			$('#iconUploadContainer').html("");
			var uploader1 = buildPlupload({
				id : "iconUploadContainer",
				elmName : "icon_id",
				targetUrl : "{{ctx}}/mam/attachment/icon",
				defaultVal : "",//设置图标
				btnName : "上传图标"
			});
		}else {
			$('#packUploadContainertr').show();
			$('#packUploadContainerInput').val("");
			$('#toboyId').hide();
			$('#localtr').hide();
			$('#local').removeAttr("btvd-type");
			$('#autoDownloadtr').show();
			$('#isAutoShowChecktr').show();
			$('#iconUploadContainertr').show();
		}
	}
</script>

